# -*- coding: utf-8 -*-
"""
Created on Sat Oct  9 12:21:20 2021

@author: 刘长奇
"""

import matplotlib.pyplot as plt 
from sklearn.datasets import load_digits
from sklearn.decomposition import PCA
import numpy as np
import random
from sklearn.metrics import confusion_matrix
import seaborn as sns
from sklearn.neural_network import MLPClassifier

# load data
digits = load_digits()

# copied from notebook 02_sklearn_data.ipynb
fig = plt.figure(figsize=(6, 6))  # figure size in inches
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)

pca = PCA(n_components=2, svd_solver="randomized")     #降成二维数据
proj = pca.fit_transform(digits.data)         #降维后的数据赋给proj

#分开测试集和数据集    3：7
num=int(0.7*np.shape(proj)[0])
train=[]
train_label=[]
test=[]
test_label=[]
arg=random.sample(range(0,np.shape(proj)[0]),num)
for i in range(num):
    train.append(proj[arg[i]])
    train_label.append(digits.target[arg[i]])
for i in range(np.shape(proj)[0]):
    if i in arg:
        continue
    else:
        test.append(proj[i])
        test_label.append(digits.target[i])
test=np.array(test)
train=np.array(train)
train_label=np.array(train_label)

#神经网络拟合
clf_class= MLPClassifier(solver='lbfgs', alpha=1e-8,hidden_layer_sizes=(200,500), random_state=1)
clf_class.fit(train,train_label)

#统计准确率
y_pred=[]
j=0
for i in range(np.shape(train)[0]):
    y_pred.append(clf_class.predict([train[i]]))
for i in range(np.shape(train)[0]):
    if y_pred[i]==train_label[i]:
        j=j+1
print('神经网络训练的训练集准确度：',j/np.shape(train)[0])    #0.68左右
y_pred=[]
j=0
for i in range(np.shape(test)[0]):
    y_pred.append(clf_class.predict([test[i]]))
for i in range(np.shape(test)[0]):
    if y_pred[i]==test_label[i]:
        j=j+1
print('神经网络训练的测试集准确度：',j/np.shape(test)[0])    #0.65左右

confusion_matrix_result=confusion_matrix(y_pred,test_label)
plt.figure(figsize=(8,6))
sns.heatmap(confusion_matrix_result,annot=True,cmap='Blues')
plt.xlabel('Predicted labels')
plt.ylabel('True labels')
plt.title('confusion_matrix')
plt.show()
